<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class ListController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $db = \DB::table("list");
		//封装搜索条件
		$params = array();
		if(!empty($_GET['name'])){
			$db->where("name","like","%{$_GET['name']}%");
			$params['name'] = $_GET['name']; //维持搜索条件
		}
		
		$list = $db->paginate(5);
		
		return view("admin.list.index",['list'=>$list,'params'=>$params]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.list.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
		
        if($request->hasFile('ufile')){
			
    		//获取上传文件信息
			$file = $request->file('ufile');
			//获取上传文件的后缀名
			$ext = $file->getClientOriginalExtension();
			//随机一个新的文件名
			$filename = time().rand(1000,9999).".".$ext;
			//执行文件移动
			$newfile = $file->move("./list/uploads/",$filename);
			
			$img = new \App\Org\Image();
			$img->open("./list/uploads/{$filename}")->thumb(50,50)->save("./list/uploads/s_{$filename}");
			
			//获取要添加的数据
			$data = [
						'name'=>$request->name,
						'photo'=>$filename,
						'src'=>$request->src,
						'title'=>$request->title,
						'content'=>$request->content,
						'num'=>$request->num,
						'auth'=>$request->auth,
						'time'=>$request->time,
						'chubanshe'=>$request->chubanshe,
						'main'=>$request->main,
						'newprice'=>$request->newprice,
						'oldprice'=>$request->oldprice,
						'level'=>$request->level,
						'cuxiao'=>$request->cuxiao,
						'yushou'=>$request->yushou,
						'xiaoliang'=>$request->xiaoliang,
						'status'=>$request->status
					];
		
			//获取自增id
			$m = \DB::table("list")->insertGetId($data);
			
			$data1 = [
						'pid'=>$m,
						'goodName'=>$request->name,
						'title'=>$request->title,
						'contents'=>$request->content,
						'auth'=>$request->auth,
						'chubanshe'=>$request->chubanshe,
						'pinglun'=>$request->num,
						'cuxiao'=>$request->cuxiao,
						'jiaoyiNum'=>$request->xiaoliang,
						'chubantime'=>$request->time,
						'status'=>$request->status,
						'marketPrice'=>$request->oldprice,
						'youhuiPrice'=>$request->newprice,
					];
					
			$n = \DB::table("goodsdetail")->insertGetId($data1);
			  if ($n<=0){
                echo "<BR> is bad";
                \DB::rollback();//事务回滚
            } 
			//判断
			if($m>0){
				$info = "图书信息添加成功！";
			}else{
				$info = "图书信息添加失败！";
			}
			
			return view("admin.list.info",['info'=>$info]);
		}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = \DB::table("list")->where("id",$id)->first();
		
		return view("admin.list.show",['list'=>$data]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = \DB::table("list")->where("id",$id)->first();
		
		return view("admin.list.edit",['list'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
		
        if($request->hasFile('ufile')){
			
			$data = \DB::table('list')->select('photo')->get();
			foreach($data as $v){
				
			}
			//删除图片
			unlink("./list/uploads/{$v->photo}");  
			unlink("./list/uploads/s_{$v->photo}");
    		//获取上传文件信息
			$file = $request->file('ufile');
			//获取上传文件的后缀名
			$ext = $file->getClientOriginalExtension();
			//随机一个新的文件名
			$filename = time().rand(1000,9999).".".$ext;
			//执行文件移动
			$newfile = $file->move("./list/uploads/",$filename);
			
			$img = new \App\Org\Image();
			$img->open("./list/uploads/{$filename}")->thumb(50,50)->save("./list/uploads/s_{$filename}");
			
			//获取要添加的数据
			$data = [
						'name'=>$request->name,
						'photo'=>$filename,
						'src'=>$request->src,
						'title'=>$request->title,
						'content'=>$request->content,
						'num'=>$request->num,
						'auth'=>$request->auth,
						'time'=>$request->time,
						'chubanshe'=>$request->chubanshe,
						'main'=>$request->main,
						'newprice'=>$request->newprice,
						'oldprice'=>$request->oldprice,
						'status'=>$request->status
					];
		
			//获取自增id
			$m = \DB::table("list")->where('id','=',$id)->update($data);
			//判断
			if($m>0){
				$info = "图书信息修改成功！";
			}else{
				$info = "图书信息修改失败！";
			}
			
			return view("admin.list.info",['info'=>$info]);
		}else{
			$data = [
						'name'=>$request->name,
			
						'src'=>$request->src,
						'title'=>$request->title,
						'content'=>$request->content,
						'num'=>$request->num,
						'auth'=>$request->auth,
						'time'=>$request->time,
						'chubanshe'=>$request->chubanshe,
						'main'=>$request->main,
						'newprice'=>$request->newprice,
						'oldprice'=>$request->oldprice,
						
						'status'=>$request->status
					];
		
			//获取自增id
			$m = \DB::table("list")->where('id','=',$id)->update($data);
			//判断
			if($m>0){
				$info = "图书信息修改成功！";
			}else{
				$info = "图书信息修改失败！";
			}
			
			return view("admin.list.info",['info'=>$info]);
		}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {		
		//获取图片名
		$data = \DB::table('list')->select('photo')->get();
		foreach($data as $v){
			
		}
		//删除整条数据
         \DB::table('list')->delete($id);
		//删除图片
		unlink("./list/uploads/{$v->photo}");  
		unlink("./list/uploads/s_{$v->photo}");
		
        return redirect("admin/list");
    }
	
	public function doCu(Request $request)
	{
		$id = $_GET['id'];
		$data = \DB::table("list")->where('id',$id)->first();
		$a = $data->cuxiao;
		
		//判断
		if($a==0){
			$a = 1;
			$list = ['cuxiao'=>1
					 
					];
			$m = \DB::table("list")->where('id',$id)->update($list);
			
			if($m>0){
						$info = "操作成功！";
					}else{
						$info = "操作失败！";
					}
					
					 return redirect("admin/list")->with("err",$info);
		}
		
		if($a==1){
			$a = 0;
			$list = ['cuxiao'=>0
					 
					];
			$m = \DB::table("list")->where('id',$id)->update($list);
			
			if($m>0){
						$info = "操作成功！";
					}else{
						$info = "操作失败！";
					}
					
					 return redirect("admin/list")->with("err",$info);
		}
	}
	
	public function doYu()
	{
		$id = $_GET['id'];
		$data = \DB::table("list")->where('id',$id)->first();
		$a = $data->yushou;
		
		//判断
		if($a==0){
			$a = 1;
			$list = ['yushou'=>1
					 
					];
			$m = \DB::table("list")->where('id',$id)->update($list);
			
			if($m>0){
						$info = "操作成功！";
					}else{
						$info = "操作失败！";
					}
					
					 return redirect("admin/list")->with("err",$info);
		}
		
		if($a==1){
			$a = 0;
			$list = ['yushou'=>0
					 
					];
			$m = \DB::table("list")->where('id',$id)->update($list);
			
			if($m>0){
						$info = "操作成功！";
					}else{
						$info = "操作失败！";
					}
					
					 return redirect("admin/list")->with("err",$info);
		}
	}
}
